//
//  ModifyNickNameViewController.m
//  zhuchiren
//
//  Created by rainday on 15/1/11.
//  Copyright (c) 2015年 rainday. All rights reserved.
//

#import "ModifyNickNameViewController.h"
#import <ReactiveCocoa.h>
#import <RACEXTScope.h>
#import "MeViewModel.h"
@interface ModifyNickNameViewController ()
@property (nonatomic ,weak) IBOutlet UIView *navView;
@property (nonatomic ,weak) IBOutlet UIButton *saveButton;
@property (nonatomic ,weak) IBOutlet UITextField *textField;



- (IBAction)dismissSelf:(id)sender;
@end

@implementation ModifyNickNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _navView.layer.shadowColor = [[UIColor colorWithRed:224.0/255 green:79.0/255 blue:95.0/255 alpha:1.0] CGColor];
    _navView.layer.shadowOffset = CGSizeMake(1.0, 1.0);
    _navView.layer.shadowOpacity = .3;
    
    RAC(_saveButton,enabled) = [RACSignal combineLatest:@[_textField.rac_textSignal] reduce:^id(NSString *string){
        return @([string length]);
        
    }];
    RAC(_saveButton.titleLabel,textColor)= [RACSignal combineLatest:@[_textField.rac_textSignal] reduce:^id(NSString *string){
        return [string length]>0?[UIColor redColor]:[UIColor lightGrayColor];
        
        
    }];

    
    @weakify(self);
    [[_saveButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [[MeViewModel setNickname:_textField.text] subscribeNext:^(id x) {
            @strongify(self);
            [[NSUserDefaults standardUserDefaults]  setObject:_textField.text forKey:@"nickname"];
            [self.delegate modifyNickNameViewController:self hasFinished:YES];
            
        } error:^(NSError *error) {
            
        }];
    }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dismissSelf:(id)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
